﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.MonoRail.Framework;
using OAuth.Net.Common;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.Services;
using Rodunu.Movies.Interfaces.Subscriptions;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Activity;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.Subscriptions;
using Rodunu.Movies.Logic.User;
using Rodunu.Movies.Services.NetFlix.DataProvider.Logic;

namespace Rodunu.Movies.Web.Controllers
{
    public class ProfileController : UserBaseController
    {
        #region Private Members

        private readonly IUser _userLogic;
        private readonly IShareMovies _shareMoviesLogic;
        private readonly IServices _servicesLogic;
        private readonly ISubscriptions _subscriptionsLogic;

        #endregion

        #region Constructor

        public ProfileController()
        {
            _userLogic = new UserLogic();
            _shareMoviesLogic = new ShareMoviesLogic();
            _servicesLogic = new ServicesLogic();
            _subscriptionsLogic = new SubscriptionsLogic();
        }

        #endregion

        public void Index()
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);

            PropertyBag["CurrentTab"] = "MyInfo";
            PropertyBag["LastLoginDate"] = LastLoginDate;
            UserInfo userInfo = _userLogic.Find(User.Id);
            PropertyBag["profile"] = userInfo;
        }

        public void SavePersonal([DataBind("profile")] UserInfo profile)
        {
            SetUser();
            UserInfo altProfile = _userLogic.Find(User.Id);
            altProfile.UserName = profile.UserName;
            altProfile.FirstName = profile.FirstName;
            altProfile.LastName = profile.LastName;
            altProfile.EmailAddress = profile.EmailAddress;

            SaveWithDefaults(altProfile);

            Redirect("Profile", "Index");
        }

        public void Services()
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);

            PropertyBag["CurrentTab"] = "Services";
            PropertyBag["LastLoginDate"] = LastLoginDate;
            UserInfo userInfo = _userLogic.Find(User.Id);
            PropertyBag["profile"] = userInfo;

            NetflixDataLogic netflixDataLogic = new NetflixDataLogic();
            IList<NetflixTokenStore> tokenStore = netflixDataLogic.FindByBase("UserInfo", userInfo);
            PropertyBag["Subscriptions"] = _subscriptionsLogic.GetActiveSubscriptions(userInfo);
            PropertyBag["AccessToken"] = (from t in tokenStore
                                          where (TokenType)t.Type
                                              == TokenType.Access
                                          select t).SingleOrDefault();
            PropertyBag["AvailableServices"] = _servicesLogic.FindAvailableServices();
        }

        public void Avatar()
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);

            PropertyBag["User"] = _userLogic.Find(User.Id);

            PropertyBag["CurrentTab"] = "Avatar";
            PropertyBag["LastLoginDate"] = LastLoginDate;
        }

        public void Settings()
        {
            SetUser();
            SetPage(PageTypes.Profile);
            SetLayout(true);

            PropertyBag["CurrentTab"] = "MyInfo";
            PropertyBag["LastLoginDate"] = LastLoginDate;
            UserInfo userInfo = _userLogic.Find(User.Id);
            PropertyBag["profile"] = userInfo;

            PropertyBag["ShareMovieTypes"] = _shareMoviesLogic.FindAll();
        }

        public void SaveSettings([DataBind("profile")] UserInfo profile)
        {
            SetUser();
            UserInfo altProfile = _userLogic.Find(User.Id);

            if (profile.ShareMovies != null)
                altProfile.ShareMovies = _shareMoviesLogic.Find(profile.ShareMovies.Id);
            altProfile.ShowProfile = profile.ShowProfile;
            altProfile.AllowToViewQueue = profile.AllowToViewQueue;

            SaveWithDefaults(altProfile);

            Redirect("Profile", "Settings");
        }

        private void SaveWithDefaults(UserInfo altProfile)
        {
            altProfile.LastModifiedBy = DateTime.Now;
            altProfile.Friends = new List<Friends>();
            altProfile.Movie = new List<Movie>();
            altProfile.NetflixTokenStore = new List<NetflixTokenStore>();
            altProfile.ServiceSubscriptionHistory = new List<ServiceSubscriptionHistory>();
            altProfile.UserCollections = new List<UserCollections>();
            altProfile.UserMovies = new List<UserMovies>();
            altProfile.UserVerification = new List<UserVerification>();
            altProfile.ServiceSubscriptions = new List<ServiceSubscriptions>();

            _userLogic.Update(altProfile);

            IUserActivity userActivityLogic = new UserActivityLogic();
            userActivityLogic.Create(new UserActivity(_userLogic.Find(User.Id), "Updating Profile", DateTime.Now, (int)UserActivityTypes.UpdateProfile, altProfile.Id));
        }

    }
}
